package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.pojo.TArticle;
import com.lagou.service.TArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Calendar;
import java.util.Objects;

@Controller
public class BlogController {

    @Autowired
    private TArticleService tArticleService;

    @RequestMapping("/toLoginPage")
    public String toLoginPage(Model model) {
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }

    @RequestMapping("/blogInfos")
    public String blogInfos(Model model,
                            @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                            @RequestParam(defaultValue = "2", value = "pageSize") Integer pageSize) {

        if (Objects.isNull(pageNum) || pageNum <= 0) {
            pageNum = 1;
        }
        if (Objects.isNull(pageSize)) {
            pageSize = 2;
        }

        System.out.println("当前页是：" + pageNum + "，显示条数是：" + pageSize);

        PageInfo<TArticle> pageInfo = tArticleService.findAll(pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);

        return "client/index";
    }

}
